﻿$(function () {
    var jQuery;
    if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.7.2') {
        var scriptTag = document.createElement('script');
        scriptTag.setAttribute("type", "text/javascript");
        scriptTag.setAttribute("src",
            "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
        if (scriptTag.readyState) {
            scriptTag.onreadystatechange = function () {
                if (this.readyState == 'complete' || this.readyState == 'loaded') {
                    scriptLoadHandler();
                }
            };
        } else {
            scriptTag.onload = scriptLoadHandler;
        }
        (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(scriptTag);
    } else {
        jQuery = window.jQuery;
        main();
    }

    function scriptLoadHandler() {
        jQuery = window.jQuery.noConflict(true);
        main();
    }

    function main() {
        jQuery(document).ready(function ($) {

            $("#btnGet").click(function () {
                $.ajax({
                    url: 'Content/themes/myTheme/html/addHtml.htm',
                    success: function (html) {
                         $('div.simplemodal-wrap').html(html);
                    }
                });
                $.ajax({
                    url: '/Home/Providers?providerId=2',
                    type: 'post',
                    dataType: 'json',
                    success: getProvider
                });
                $.ajax({
                    url: '/Home/Services',
                    type: 'post',
                    dataType: 'json',
                    success: getServices
                });
            });
        });
    }
});

function getServices(services) {
    var servCol1 = '';
    var servCol2 = '';
    var s = 0;
    var n = services.length;
    $.each(services, function (k) {
        s++;
        if (s < n / 2 + 1) {
            if (s == (n / 2 - 1).toFixed(0)) {
                servCol1 += '<tr><td style=\"color:white; font-size:14px;\">' + this.ServiceName + '</td>' +
                '<th>' + 'от ' + this.ServicePrice + 'р' + '</th></tr>';
                $('#servCol1').html(servCol1);
            }
            servCol1 += '<tr><td style=\"color:white; font-size:14px;\">' + this.ServiceName + '</td>' +
            '<th>' + 'от ' + this.ServicePrice + 'р' + '</th></tr>';
        }
        if (s > n / 2 + 1) {
            if (s == n) {
                servCol2 += '<tr><td style=\"color:white; font-size:14px;\">' + this.ServiceName + '</td>' +
                '<th>' + 'от ' + this.ServicePrice + 'р' + '</th></tr>';
                $('#servCol2').html(servCol2);
            }
            servCol2 += '<tr><td style=\"color:white; font-size:14px;\">' + this.ServiceName + '</td>' +
                '<th>' + 'от ' + this.ServicePrice + 'р' + '</th></tr>';
        }
    });
    $("tr").click(function () {
        $.ajax({
            url: '/Home/Resources?serviceName=' + $(this.cells[0]).text(),
            type: 'post',
            dataType: 'json',
            success: getResources
        });
    });
}

function getResources(people) {
    var tableRes = '<table class=\"Resources2\">' + '<tr>';
    $.each(people, function (m) {
        tableRes += '<td class=\"res\"><p style=\"color:white;\" class=\"ResName\">' + this.ResourceName + '</p>' +
            '<p style=\"color:white;\" class=\"Descr\">' + this.ResourceDescription + '</p></td>';
    });
    tableRes += '<a id=\"Cansel\" style=\"float: right;\">другая услуга</a>';
    var time = InitTime();
    var button = '<input id=\"buttonEnter\" type=\"button\" value=\"Записаться\"></input>';
    $('#basic-modal-content').html(tableRes + time + button);
    $("#Cansel").click(function () {
        CancelButton();
    });
    var i, j = 0;
    $("td.res").click(function () {
        var res = document.getElementsByClassName("resActive")[0];
        if (res == undefined) {
            i++;
            this.className += " resActive";
        }
        else {
            res.className -= " resActive";
            i++;
            this.className += " resActive";
        }
    });
    $("td.spare").click(function () {
        var timeCheck = document.getElementsByClassName("tlActive")[0];
        if (timeCheck == undefined) {
            j++;
            this.firstChild.className += "tlActive";
        }
        else {
            timeCheck.className = "";
            j++;
            this.firstChild.className += "tlActive";
        }
    });
    $("#buttonEnter").click(function () {
        if (i != 0 && j != 0) {
            var timeCheck = document.getElementsByClassName("tlActive")[0].getElementsByTagName("i")[0];
            var res = document.getElementsByClassName("resActive")[0].getElementsByTagName("p")[0];
            $('#basic-modal-content').html("");
            var code = '<a id=\"Cansel\" style=\"float: right;\">другая услуга</a><div style=\"color: white; font:size: 16px;\">Спасибо, что выбрали нас!</div><div style=\"color: white; font:size: 16px;\">Выбранное время: <b>' + timeCheck.childNodes[0].data + '</b><br />' + 'Выбранный специалист: <b>' + res.childNodes[0].data + '</b></div>';
            $("#basic-modal-content").html(code);
            $("#Cansel").click(function () {
                CancelButton();
            });
        } else {
            alert("Выберите время и специалиста!");
        }
    });
}

function getProvider(provider) {
    $.each(provider, function (i) {
        $('#provName').html(this.ProviderName);
        $('#provAddress').html(this.ProviderAddress + '  <b>|</b>  ' + 'тел. ' + this.ProviderPhone);
    });
}

function InitTime() {
    var time = '<h3 style=\"color: white;\">' + "Свободное время" + '</h3><div class=\"tlTimelineAndCalendar\"><div class=\"tlTimeLine\"><table class=\"tlTime\"><tbody><tr><th>' + "Утро" + '</th><td class=\"tlBusy\">' + ". ." + '</td><td class=\"tlBusy\">' + ". ." + '</td><td class=\"tlBusy\">' + ". ." + '</td><td class=\"tlBusy\">' + ". ." + '</td><td class=\"tlBusy\">' + ". ." + '</td><td class=\"tlBusy\">' + ". ." + '</td><td class=\"tlBusy\">' + ". ." + '</td><td class=\"tlBusy\">' + ". ." + '</td><td class=\"tlBusy\">' + ". ." + '</td><td class=\"tlBusy\">' + ". ." + '</td></tr><tr><th>' + "День" + '</th><td class=\"tlBusy\">' + ". ." + '</td><td class=\"tlBusy\">' + ". ." + '</td><td class=\"tlBusy\">' + ". ." + '</td><td class=\"spare\"><a class=\"\"><i>' + '14:30' + '</i></a></td><td class=\"spare\"><a class=\"\"><i>' + '15:00' + '</i></a></td><td class=\"spare\"><a class=\"\"><i>' + '15:30' + '</i></a></td><td class=\"spare\"><a class=\"\"><i>' + '16:00' + '</i></a>' + '</td><td class=\"spare\"><a class=\"\"><i>' + '16:30' + '</i></a>' + '</td><td class=\"spare\"><a class=\"\"><i>' + '17:00' + '</i></a>' + '</td><td class=\"spare\">' + '<a class=\"\"><i>' + '17:30' + '</i></a>' + '</td>' + '</tr><tr><th>' + 'Вечер' + '</th><td class=\"spare\"><a class=\"\"><i>' + '18:00' + '</i></a>' + '</td><td class=\"spare\"><a class=\"\"><i>' + '18:30' + '<i></a>' + '</td><td class=\"spare\"><a class=\"\"><i>' + '19:00' + '</i></a>' +
    '</td><td class=\"spare\"><a class=\"\"><i>' + '19:30' + '</i></a></td><td class=\"spare\"><a class=\"\"><i>' + '20:00' + '</i></a>' + '</td><td class=\"tlBusy\">' + ". ." + '</td></tr></tbody></table></div></div>';
    return time;
}

function CancelButton() {
    $('#basic-modal-content').html("");
    $.ajax({
        url: 'Content/themes/myTheme/html/cancelHtml.htm',
        success: function (html) {
             $('#basic-modal-content').html(html);
        }
    });
    $.ajax({
        url: '/Home/Providers?providerId=2',
        type: 'post',
        dataType: 'json',
        success: getProvider
    });
    $.ajax({
        url: '/Home/Services',
        type: 'post',
        dataType: 'json',
        success: getServices
    });
}